package 面向对象.实验9;

/**
 * @author IT_Jay
 * @date 2021/12/2 10:54
 */

public class Test02 {
    public static void main(String[] args) {
        Tickets tickets = new Tickets(20);
        Thread thread1 = new Thread(tickets,"售票点1");
        Thread thread2 = new Thread(tickets,"售票点2");
        Thread thread3 = new Thread(tickets,"售票点3");
        thread1.start();
        thread2.start();
        thread3.start();

    }
}
class Tickets extends Thread{
    int num;
    public Tickets(int num){
        this.num=num;
    }
    public void run(){

        while (true){
            try {
                Thread.sleep((long) (Math.random()*1000));
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            synchronized (this) {       //加锁
            if (num>0){
                System.out.println(Thread.currentThread().getName()+"正在售第"+ num-- +"张票");
            }
            if (num == 0) break;
        }
        }
    }
}
//class Tickets extends Thread
//{
//    int num;
//    public Tickets(int num)
//    {
//        this.num=num;
//    }
//    public void run()
//    {
//        while(true)
//        {
//            try {
//                Thread.sleep((long)(Math.random()*1000));
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//            if(num>0)
//                System.out.println(Thread.currentThread().getName()+"正在售第"+num--+"张票");
//            if(num==0) break;
//        }
//    }
//}

